﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _10_HPBar
{
    internal class InfoSystem
    {
        // 单例模式
        // 保证类的实例只有一个
        // 能在任何地方直接调用
        //static InfoSystem? instance = null;
        //public static InfoSystem Instance { get 
        //    { 
        //        // 如何保证唯一性 就是让构造函数私有
        //        // 只能在第一次调用时自己实例化
        //        if(instance == null)
        //        {
        //            instance = new InfoSystem(30, 2, 12, 20, 50);                
        //        }
        //        return instance; 
        //    } 
        //}

        // 获得消息
        // 存储消息
        // 使用字符串数组
        // 打印消息
        struct InfoData
        {
            public string str;
            public ConsoleColor color;
        }

        InfoData[] infoList;
        int x, y;
        int row, col;
        int index;
        int maxCount;
        bool isShow;

        long lastTime, currentTime;
        int delay;

        public InfoSystem(int x, int y, int row, int col/*, int delay*/)
        {
            //Console.CursorVisible = false;
            this.x = x;
            this.y = y;
            this.row = row;
            this.col = col;
            index = 0;
            maxCount = row - 2;
            infoList = new InfoData[maxCount];
            isShow = true;

            //this.delay = delay * 10000;
            //lastTime = DateTime.Now.Ticks;
        }

        public void AddInfo(string str, ConsoleColor color = ConsoleColor.White)
        {
            if (index > maxCount - 1)
            {
                index = maxCount - 1;
                ForwardInfoArray();
            }
            InfoData info = new InfoData();
            info.str = str;
            info.color = color;
            infoList[index++] = info;
        }

        void ForwardInfoArray()
        {
            for (int i = 0; i < maxCount - 1; i++)
            {
                infoList[i] = infoList[i + 1];
            }
        }

        void PrintFrame(int row, int col, int x, int y, ConsoleColor color = ConsoleColor.White, bool clear = true)
        {
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    string temp = "";
                    if (i == 0 && j == 0)
                    {
                        temp = "┌";
                    }
                    else if (i == 0 && j == col - 1)
                    {
                        temp = "┐";
                    }
                    else if (i == row - 1 && j == 0)
                    {
                        temp = "└";
                    }
                    else if (i == row - 1 && j == col - 1)
                    {
                        temp = "┘";
                    }
                    else if (i == 0 || i == row - 1)
                    {
                        temp = "─";
                    }
                    else if (j == 0 || j == col - 1)
                    {
                        temp = "│";
                    }
                    else if (clear)
                    {
                        temp = "  ";
                    }
                    MyPrint(temp, x + j, y + i, color);
                }
            }
        }

        void PrintText()
        {
            for (int i = 0; i < row - 2; i++)
            {
                MyPrint(infoList[i].str, x + 3, y + 1 + i, infoList[i].color);
            }
        }

        public void Render()
        {
            if (!isShow)
                return;
            PrintFrame(row, col, x, y);
            PrintText();
        }

        void MyPrint(string str, int x, int y, ConsoleColor color = ConsoleColor.White)
        {
            Console.SetCursorPosition(x * 2, y);
            Console.ForegroundColor = color;
            Console.Write(str);
            Console.ResetColor();
        }

        public void Show()
        {
            isShow = !isShow;
            Clear();
        }

        void Clear()
        {
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    MyPrint("  ", x + j, y + i);
                }
            }
        }

        //public void Update()
        //{
        //    currentTime = DateTime.Now.Ticks;
        //    if (currentTime - lastTime > delay)
        //    {
        //        if (isOpen)
        //        {
        //            row++;
        //            if (row > 12) row = 12;
        //        }
        //        else
        //        {
        //            row--;
        //            if (row < 0) row = 0;
        //        }
        //        lastTime = DateTime.Now.Ticks;
        //    }
        //}
    }
}